package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.CourseTextbook;
import com.xmy.cultivate.entity.CourseType;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.service.ICourseTextbookService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 * 教材 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-07
 */
@RestController
@RequestMapping("/api/courseTextbook")
public class CourseTextbookController {

    @Autowired
    ICourseTextbookService iCourseTextbookService;

    @GetMapping()
    public Result findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                          @RequestParam("yearPart") Integer yearPart,
                          @RequestParam("quarterNum") Integer quarterNum,
                          @RequestParam("subjectsId") Long subjectsId,
                          @RequestParam("classTypeId") Long classTypeId,
                          @RequestParam("yearClassId") String yearClassId
                          ) {

        IPage<CourseTextbook> iPage = new Page<>(pageNum,pageSize);

        QueryWrapper<CourseTextbook> courseTextbookQueryWrapper = new QueryWrapper<>();
        courseTextbookQueryWrapper.select("*,getDictName(year_class_id, 14) as year_class_name,getCourseTypeName(class_type_id) as class_type_name");
        courseTextbookQueryWrapper.eq(yearPart != null,"year_part", yearPart);
        courseTextbookQueryWrapper.eq(quarterNum != null,"quarter_num", quarterNum);
        courseTextbookQueryWrapper.eq(subjectsId != null,"subjects_id", subjectsId);
        courseTextbookQueryWrapper.eq(classTypeId != null,"class_type_id", classTypeId);
        courseTextbookQueryWrapper.eq(StringUtils.isNotBlank(yearClassId),"year_class_id", yearClassId);
        courseTextbookQueryWrapper.orderByDesc("id");
        iCourseTextbookService.page(iPage, courseTextbookQueryWrapper);

        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        CourseTextbook courseTextbook = iCourseTextbookService.getById(id);
        return new Result(Code.OK,courseTextbook,"");
    }

    @PostMapping()
    public Result save(@RequestBody CourseTextbook courseTextbook) {
        iCourseTextbookService.save(courseTextbook);
        return new Result(Code.OK,"",Code.OK_MSG);
    }

    @PutMapping()
    public Result update(@RequestBody CourseTextbook courseTextbook) {
        iCourseTextbookService.updateById(courseTextbook);
        return new Result(Code.OK,"",Code.OK_MSG);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        iCourseTextbookService.removeById(id);
        return new Result(Code.OK,"",Code.OK_MSG);
    }
}
